/*
* 游戏中心
*/
class Game {
    //记录关卡
    private _level: number = 1;
    //运行数据
    public _state: number = Define.Game_No;//状态
    public mapView: ui.map.MapViewUI;
    public enemys: Array<Enemy> = [];//怪物集合
    public generals: Array<General> = [];//英雄集合
    public bullets: Array<Bullet> = [];//子弹集合
    
    private levelEnemy: LevelEnemy;

    public endNode: ANode = new ANode(24, 5);//终点
    public NODES: number[][];//地图
    public enemySpeed: number = 1;
    public skillId: number = 0;//准备释放的技能
    public init() {
        this.NODES = Define.NODES;
        //1加载关卡数据
        Laya.LocalStorage.setItem("level",'6');
        let level = Laya.LocalStorage.getItem("level");
        if (level) {
            this._level = Number(level);
        }
        let state = Laya.LocalStorage.getItem("state");
        if (state) {
            this._state = Number(state);
        }
       


    }
    /**
     * 循环方法
     */
    private loop(): void {
        // // //修改怪物位置
        // for (let i = this.enemys.length - 1; i >= 0; i--) {
        //     let enemy = this.enemys[i];
        //     //console.log('敌人动了 +'+ i);
        //     enemy.updatePos();
        // }

        // if (this.enemys.length == 0 && this.levelEnemy.enemys.length == 0) {
        //     // console.log('nextLevel')
        //     this.nextLevel();
        // }else{
        //     // console.log('this.enemys +' +this.enemys)
        //     // console.log('this.levelEnemy.enemys + '+this.levelEnemy.enemys)
        // }
        // //刷新怪物
        // this.flushEnemy();


        //修改怪物位置
        for (let i = this.enemys.length - 1; i >= 0; i--) {
            let enemy = this.enemys[i];
            enemy.updatePos();
        }
        //检测子弹攻击
        for (let i = this.bullets.length - 1; i >= 0; i--) {
            let bullet = this.bullets[i];
            bullet.updatePos();
        }

        //刷新怪物
        this.flushEnemy();
        //检测攻击
        this.generals.forEach((val, idx, array) => {
            if (val.isHit()) {
                let gen = val.tb;
                let harm = gen.zharm;
                let distance = gen.distance;
                let harmType = gen.harmType;
                let range = gen.range;
                let posx = val.posx;
                let posy = val.posy;
                if (harmType == Define.Harm_Bullet) {
                    let enemy = this.findEnemy(distance, posx, posy);
                    if (enemy) {
                        val.hitEnemy();
                        let bullet = new Bullet(enemy, gen.bulletType, harm, gen.range, posx, posy);
                        this.bullets.push(bullet);
                    }
                } else if (harmType == Define.Harm_Nomeral) {
                    if (range > 0) {
                        let arr = this.findEnemys(distance, posx, posy);
                        if (arr.length > 0) {
                            val.hitEnemy();
                            arr.forEach((val, idx, array) => {
                                //播放被攻击特效
                                var hit = new Laya.Animation();
                                hit.loadAnimation("hit.ani");
                                hit.pos(val.width / 2, val.height / 2);
                                hit.play();
                                val.addChild(hit);
                                Laya.timer.once(500, val, function () {
                                    hit.removeSelf();
                                    hit.destroy();
                                });
                                val.harmHp(harm);
                            });
                        }
                    } else {
                        let enemy = this.findEnemy(distance, posx, posy);
                        if (enemy) {
                            val.hitEnemy();
                            var hit = new Laya.Animation();
                            hit.loadAnimation("hit.ani");
                            hit.pos(enemy.width / 2, enemy.height / 2);
                            hit.play();
                            enemy.addChild(hit);
                            Laya.timer.once(500, enemy, function () {
                                hit.removeSelf();
                                hit.destroy();
                            });
                            enemy.harmHp(harm);
                        }
                    }
                }
            }
        });
        if (this.enemys.length == 0 && this.levelEnemy.enemys.length == 0) {
            this.nextLevel();
        }
    }
    /**
     * 开始游戏
     */
    public startGame(): void {

        Laya.timer.loop(Define.UpdateTime, this, this.loop);


        this.enemys = [];
        console.log('###############this._level');
        console.log(this._level);
        this.levelEnemy = new LevelEnemy(this._level);
 
        
       
    }
    /**
      * 暂停游戏
      */
    public stop(): void {
        console.log("暂停游戏");
        Laya.timer.clear(this, this.loop);
    }
    /**
    * 暂停结束
    */
    public stopover(): void {
         console.log("暂停结束");
        Laya.timer.loop(Define.UpdateTime, this, this.loop);
    }


    /**
     * 关卡开始表现
     */
    public nextLevel(): void {
        game.stop();
        Laya.timer.clear(this, this.loop);
        console.log("进入下一关");
        this._level = this._level + 1;
        game.startGame();
    }
    /**
     * 怪物变速
     */
    public enemySpeedUp(enemySpeed: number): void {
        console.log("怪物加速:" + enemySpeed);
        this.enemySpeed = enemySpeed;
    }

     /**
     * 刷新怪物
     */
    private flushEnemy(): void {
        if (!this.levelEnemy) {
            return;
        }
        //刷新怪物
        this.levelEnemy.flushEnemy();
        //移动怪物
    }

     /**
     * 游戏结束
     */
    public gameOver(): void {
        Laya.timer.clear(this, this.loop);
        console.log("游戏结束");

        
        this.nextLevel();
    }

     /**
     * 测试点击
     */
    public upgradeTower(): void {
        console.log("upgradeTower");
    }
}
let game: Game;